#!/bin/bash
# Author: Auroot/BaSierl
# QQ： 2763833502
# Description： Arch Linux 安装脚本 
# URL Blog： https://basierl.github.io
# URL GitHub： https://github.com/BaSierL/arch_install.git
# URL Gitee ： https://gitee.com/auroot/arch_install.git
clear;
#==============-------------mirrorlist----------------==============
cat > /etc/pacman.d/mirrorlist << EOF
################################################################################
################# Arch Linux mirrorlist generated by Reflector #################
################################################################################

# With:       reflector --verbose --country China -l 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist 
# With:       reflector --verbose --country China -l 20 --protocol http --sort rate --save /etc/pacman.d/mirrorlist 
# From:       https://www.archlinux.org/mirrors/status/json/

## China
Server = https://mirrors.sjtug.sjtu.edu.cn/archlinux/\$repo/os/\$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch
Server = http://mirrors.163.com/archlinux/\$repo/os/\$arch

## China
Server = https://mirrors.163.com/archlinux/\$repo/os/\$arch
Server = http://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch

EOF
echo "$(date -d "2 second" +"%Y-%m-%d %H:%M:%S")  mirrorlist=yes" >> /tmp/Arch_install.log

#==============-------------pacman.conf----------------==============
cat > /etc/pacman.conf << EOF
# /etc/pacman.conf
#
# See the pacman.conf(5) manpage for option and repository directives
#
# GENERAL OPTIONS
#
[options]
# The following paths are commented out with their default values listed.
# If you wish to use different paths, uncomment and update the paths.
#RootDir     = /
#DBPath      = /var/lib/pacman/
#CacheDir    = /var/cache/pacman/pkg/
#LogFile     = /var/log/pacman.log
#GPGDir      = /etc/pacman.d/gnupg/
#HookDir     = /etc/pacman.d/hooks/
HoldPkg     = pacman glibc
#XferCommand = /usr/bin/curl -L -C - -f -o %o %u
#XferCommand = /usr/bin/wget --passive-ftp -c -O %o %u
CleanMethod = KeepInstalled
#UseDelta     = 0.7
Architecture = auto

#IgnorePkg   =
#IgnorePkg   =
#IgnoreGroup =

#NoUpgrade   =
#NoExtract   =

# Misc options
UseSyslog
Color
TotalDownload
CheckSpace
VerbosePkgLists

# By default, pacman accepts packages signed by keys that its local keyring
# trusts (see pacman-key and its man page), as well as unsigned packages.
SigLevel    = Required DatabaseOptional
LocalFileSigLevel = Optional
RemoteFileSigLevel = Required

# NOTE: You must run "pacman-key --init" before first using pacman; the local
# keyring can then be populated with the keys of all official Arch Linux
# packagers with "pacman-key --populate archlinux".
#
# REPOSITORIES
#   - can be defined here or included from another file
#   - pacman will search repositories in the order defined here
#   - local/custom mirrors can be added here or in separate files
#   - repositories listed first will take precedence when packages
#     have identical names, regardless of version number
#
# Repository entries are of the format:
#       [repo-name]
#       Server = ServerName
#       Include = IncludePath
#
# The header [repo-name] is crucial - it must be present and
# uncommented to enable the repo.
#
# 默认情况下，测试存储库被禁用。若要启用，请取消注释
# repo name header and Include lines. You can add preferred servers immediately
# after the header, and they will be used before the default mirrors.

#[testing]
#Include = /etc/pacman.d/mirrorlist

[core]
Include = /etc/pacman.d/mirrorlist

[extra]
Include = /etc/pacman.d/mirrorlist

#[community-testing]
#Include = /etc/pacman.d/mirrorlist

[community]
Include = /etc/pacman.d/mirrorlist

# 如果要在x86_64系统上运行32位应用程序，
# 根据需要在此启用多库存储库。

#[multilib-testing]
#Include = /etc/pacman.d/mirrorlist

[multilib]
Include = /etc/pacman.d/mirrorlist

# ArchLinux CN 腾讯源
[archlinuxcn]
Server = https://mirrors.cloud.tencent.com/archlinuxcn/\$arch

# ArchLinux CN 腾讯源
# [archlinuxcn]
# Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/\$arch

# BlackArch 官方源 渗透工具源
# [blackarch]
# SigLevel = Optional TrustAll
# Server = https://mirrors.tuna.tsinghua.edu.cn/blackarch/\$repo/os/\$arch

# ArcoLinux 官方GitHub源 1
# [arcolinux_repo]
# SigLevel = Required DatabaseOptional   # 必需数据库可选
# SigLevel = Optional TrustAll            #可选信任
# Server = https://arcolinux.github.io/arcolinux_repo/\$arch

# ArcoLinux 官方GitHub源 2
# [arcolinux_repo_iso]
# SigLevel = Required DatabaseOptional   # 必需数据库可选
# SigLevel = Optional TrustAll            #可选信任
# Server = https://arcolinux.github.io/arcolinux_repo_iso/\$arch

# Arch4edu 是面向高校用户推出的非官方软件仓库， 支持 Arch Linux 和 Arch Linux ARM， 主要包含高校用户常用的科研、教学及开发软件。
# [arch4edu]  
# SigLevel = TrustAll
# Server = http://mirrors.tuna.tsinghua.edu.cn/arch4edu/\$arch

# [arcolinux_repo_3party]
# SigLevel = Required DatabaseOptional
# Include = /etc/pacman.d/arcolinux-mirrorlist

# [arcolinux_repo_xlarge]
# SigLevel = Required DatabaseOptional
# Include = /etc/pacman.d/arcolinux-mirrorlist

# An example of a custom package repository.  See the pacman manpage for
# tips on creating your own repositories.
# 本地镜像源
#[custom]
#SigLevel = Optional TrustAll
#Server = file:///home/custompkgs
EOF
echo "$(date -d "2 second" +"%Y-%m-%d %H:%M:%S")  archlinuxcn=yes" >> /tmp/Arch_install.log
sleep 1;
# 判断/tmp/Arch_install.log日志文件中是否有"archlinuxcn-keyring=yes"
# [存在]  退出  \ [不存在] 更新archlinuxcn-keyring并记录
if $(grep -q "archlinuxcn-keyring=yes" /tmp/Arch_install.log) ; then
    clear;
    exit 0;
else
    sudo pacman -Syy archlinuxcn-keyring
    echo "$(date -d "2 second" +"%Y-%m-%d %H:%M:%S")  archlinuxcn-keyring=yes" >> /tmp/Arch_install.log
fi

#curl -fsSL https://gitee.com/auroot/Arch_install/tree/master > /tmp/Script_Update_initialize
#cat /tmp/Script_Update_initialize 2&> /dev/null | awk 'NR==1141{print}' | cut -d"'" -f6 | tr -cd "[0-9]" | cut -d"%" -f1 > /tmp/Script_Update_initialize
